layui.config({
    base: "/js/modules/"
}).extend({
    "listView": "listView"
});
layui.use(['form', 'listView', 'laydate', 'jquery', 'common','table', 'treeSelect'], function () {
    let layDate = layui.laydate,
        form = layui.form,
        common = layui.common,
        layer = layui.layer,
        treeSelect = layui.treeSelect,
        table = layui.table,
        $ = layui.$;
    treeSelect.render({
        // 选择器
        elem: '#tree',
        // 数据
        data: '/device/type/tree',
        // data: '/data/data3.json',
        // 异步加载方式：get/post，默认get
        type: 'get',
        // 占位符
        placeholder: '选择设备类型',
        // 是否开启搜索功能：true/false，默认false
        search: true,
        // 点击回调
        click: function (d) {
            $("#tree").val(d.current.id);

        },
        // 加载完成后的回调函数
        success: function (d) {
        }
    });
    layDate.render(
        {
            elem: "#beginTime", trigger: 'click',
            type: 'datetime'
        }
    );
    layDate.render(
        {
            elem: "#endTime", trigger: 'click',
            type: 'datetime'
        }
    );

    layui.listView.tableInit({
        id: 'warnRule',
        url: 'rule/page',
        page: true,
        cols: [
            {field: 'number', title: '序号', fixed: 'true', type: 'numbers'},
            {field: 'deviceType', hide: true,title: '设备类型'},
            {field: 'paramsId',hide: true,title: '告警参数'},
            {field: 'id',hide: true, title: 'id'},
            {field: 'deviceTypeName',title: '设备类型'},
            {field: 'deviceTypeDataName',title: '告警参数'},
            {field: 'duration', title: '持续时间'},
            {field: 'minValue', title: '最小值'},
            {field: 'maxValue', title: '最大值'},
            {
                field: 'valid', title: '是否生效', templet: function (d) {
                    return d.valid ? "是" : "否"
                }
            },
            {field: 'creator', title: '创建人'},
            {field: 'createTime', title: '创建时间'},
            {title: '操作', templet: '#operateBar', fixed: "right", align: "center"}
        ]
    });

    $('#add').on('click', function () {
        layer.open({
            title: '新增告警规则',
            type: 2,
            area: ['600px', '400px'],
            fixed: false, //不固定
            maxmin: true,
            content: 'addWanRule'
        });
    });

    table.on('tool(warnRule)', function (obj) {
        const data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                title: '编辑告警规则',
                type: 2,
                area: ['600px', '350px'],
                fixed: false, //不固定
                maxmin: true,
                content: 'splitRule/addSplitRule?id=' + data.id
            });
        } else if (obj.event === 'delete') {
            const msg = '真的要刪除？';
            layer.confirm(msg, {icon: 0, title: '提示信息'}, function (index) {
                layui.$.post("delete", {id: data.id})
                    .then(function (result) {
                        console.log(result);
                        layer.msg(result.message);
                        layer.close(index);
                        if (result.code == '200') {
                            obj.del();
                            $("#query").click();
                        }
                    });
            });
        }
    });
});
